package com.juli.v2xdemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.cz.basetool.CodeTool;
import com.juli.v2xdemo.R;
import com.juli.v2xdemo.entity.CarInfo;
import com.juli.v2xdemo.entity.MessageEvent;
import com.juli.v2xdemo.presenter.TcpPresenter;
import com.juli.v2xdemo.util.CoodUtils;
import com.juli.v2xdemo.util.GPSutils;

import de.greenrobot.event.EventBus;

public class TcpActivity extends Activity {

    TextView tvNetState;
    TcpPresenter tcpPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tcp);
        EventBus.getDefault().register(this);
        tvNetState = (TextView) findViewById(R.id.tvNetState);
        Button button = (Button) findViewById(R.id.button);
//        tcpPresenter = new TcpPresenter(this, new TcpActionCallBack(this), new TcpUICallBackImpl(this, tvNetState));
//        PresenterManager.getInstance().registTcpPresenter(tcpPresenter);
//        PresenterManager.getInstance().init();
//        button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                tcpPresenter.send();
//            }
//        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    public void onEventMainThread(MessageEvent event) {
        Log.e("MessageEvent", "xxxx");
        if (!TextUtils.isEmpty(event.getType())) {
            switch (event.getType()) {
                case "data":
                    if (!TextUtils.isEmpty(event.getType())) {
//                        Log.e("MessageEvent", "event"+event.getMessage());
                        CarInfo carData = event.getCarInfo();
                        String vehichlInfo = carData.getVehichlInfo();

                        String lat_long_dir = carData.getLat_Long_Dir();


                        String vehichlInfo8 = CodeTool.oneByteHexNum2BinaryStr8(vehichlInfo);
                        String isme = vehichlInfo8.substring(0, 1);
                        String early_warning = vehichlInfo8.substring(1, 8);


                        if (isme.equals("0")) {
                            //自己
                            //预警
                            if (early_warning.equals("0000000")) {

                                Log.e("预警", "无预警");
                            } else if (early_warning.equals("0000001")) {
                                Log.e("预警", "前方车辆碰撞预警");
                            } else if (early_warning.equals("0000010")) {
                                Log.e("预警", "后方盲区超车预警");
                            } else if (early_warning.equals("0000100")) {
                                Log.e("预警", "紧急车辆预警");
                            } else if (early_warning.equals("0001000")) {
                                Log.e("预警", "故障车辆预警预警");
                            }


                            //经度
                            String latitude = GPSutils.longitudeandlatitude(carData.getLatitude());
                            //纬度
                            String longitude = GPSutils.longitudeandlatitude(carData.getLongitude());

                            Log.e("纬度latitude", "" + latitude);
                            Log.e("经度longitude", "" + longitude);
                            //经纬度方向
                            String Lat_Long_Dir = carData.getLat_Long_Dir();
                            String Lat_Long_Dir8 = CodeTool.oneByteHexNum2BinaryStr8(Lat_Long_Dir);
                            Log.e("经纬度方向Lat_Long_Dir8", Lat_Long_Dir8);
                            String Heading = carData.getHeading();
                            Log.e("车辆航向角Heading", Heading);
                            //速度
                            String Speed = carData.getSpeed();
                            String sudu = CoodUtils.Sudu(Speed);
                            Log.e("速度sudu", sudu);
                            String AlertType = carData.getAlertType();
                            String baty1 = AlertType.substring(0, 2);
                            String baty2 = AlertType.substring(2, 4);
                            String baty28 = CodeTool.oneByteHexNum2BinaryStr8(baty2);
                            Log.e("路测警示AlertType", baty28);
                            int v = Integer.parseInt(baty1);
                            AlertType_Byte1( v);
                            AlertType_Byte2(baty28);
                            //警示信息距离
                            String description = carData.getDescription();

                            Description(description);
                            //车道数目
                            String Lane_num = carData.getLane_num();
                            //当前车辆所在车道
                            String veh_lane = carData.getVeh_Lane();

                            Log.e("我车车道", "" + veh_lane);

                        } else {


                            //经度
                            String latitude = GPSutils.longitudeandlatitude(carData.getLatitude());
                            //纬度
                            String longitude = GPSutils.longitudeandlatitude(carData.getLongitude());
                            //当前车辆所在车道
                            String veh_lane = carData.getVeh_Lane();
                            Log.e("他车纬度latitude", "" + latitude);
                            Log.e("他车经度longitude", "" + longitude);
                            Log.e("他车车道", "" + veh_lane);
                            //別人
                        }
                        Log.e("MessageEvent", "event:" + vehichlInfo8);

                    } else {
                        Log.e("MessageEvent", "event数据为空");
                    }
                    break;

                default:
                    break;
            }


        }
    }

    //0为无预警  1警示信息的距离，2为红灯预警 ，3为限速预警
    int type = 0;

    public void AlertType_Byte1(int early_warning) {

        switch (early_warning) {
            case 0:
                Log.e("路测警示", "无预警");
                type = 0;
                break;
            case 37:
                Log.e("路测警示", "危险");
                type = 1;
                break;
            case 15:
                Log.e("路测警示", "落石");
                break;
            case 2:
                Log.e("路测警示", "急弯");
                type = 1;
                break;
            case 38:
                Log.e("路测警示", "正在施工");
                type = 1;
                break;
            case 17:
                Log.e("路测警示", "路滑");
                type = 1;
                break;
            case 21:
                Log.e("路测警示", "隧道");
                type = 1;
                break;
            case 27:
                Log.e("路测警示", "注意危险");
                type = 1;
                break;
            case 85:
                Log.e("路测警示", "限速");
                type = 1;
                break;

            default:
                break;
        }

    }

    public void AlertType_Byte2(String early_warning) {

        if (early_warning.substring(0, 1).equals("1")) {
            Log.e("路测警示", "交叉路口碰撞预警");
            type = 1;
        } else {
            Log.e("路测警示", "交叉路口无预警");
            type = 0;
        }

        if (early_warning.substring(1, 2).equals("1")) {
            Log.e("路测警示", "直行红灯预警");
            type = 2;
        } else {
            Log.e("路测警示", "直行红灯无预警");
            type = 0;
        }
        if (early_warning.substring(2, 3).equals("1")) {
            Log.e("路测警示", "左转红灯预警");
            type = 2;
        } else {
            Log.e("路测警示", "左转红灯无预警");
            type = 0;
        }
        if (early_warning.substring(3, 4).equals("1")) {
            Log.e("路测警示", "右转红灯预警");
            type = 2;
        } else {
            Log.e("路测警示", "右转红灯无预警");
            type = 0;
        }
        if (early_warning.substring(4, 5).equals("1")) {
            Log.e("路测警示", "前方行人预警");
            type = 1;
        } else {
            Log.e("路测警示", "前方行人无预警");
            type = 0;
        }
        if (early_warning.substring(5, 6).equals("1")) {
            Log.e("路测警示", "限速预警");
            type = 3;
        } else {
            Log.e("路测警示", "限速无预警");
            type = 0;
        }

    }

    public void Description(String s) {
        if (type == 0) {
            //路况无预警

        } else if (type == 1) {
            //警示信息的距离(米)
            long parseLong = Long.parseLong(s,16);
            Log.e("路测警示距离",""+ parseLong);
        } else if (type == 2) {
            //红灯预警
            String baty1 = s.substring(0, 2);
            long l = Long.parseLong(baty1);
            if (l==1) {
                //红灯
                Log.e("红灯警示","");
            } else if (l==2) {
                //黄灯
                Log.e("黄灯警示","");
            } else if (l==3) {
                //绿灯
                Log.e("绿灯警示","");
            }
            String baty2 = s.substring(2, 4);
            //倒计时时间
            long l2 = Long.parseLong(baty2);
            Log.e("警示倒计时",""+l2);
        } else if (type == 3) {
            //限速类型
            String baty1 = s.substring(0, 2);
             int v = Integer.parseInt(baty1);


            //======================限速预警===================
            switch (v) {
                case 0:
                    Log.e("限速预警","无限速" );
                    break;
                case 1:
                    Log.e("限速预警","学校区域限速" );
                    break;
                case 2:
                    Log.e("限速预警","学校放学时的最大速度" );
                    break;
                case 3:
                    Log.e("限速预警","建筑区域最大速度" );
                    break;
                case 4:
                    Log.e("限速预警","乘用车最小速度" );
                    break;
                case 5:
                    Log.e("限速预警","乘用车最大速度" );
                    break;
                case 6:
                    Log.e("限速预警","乘用车夜晚最大速度" );
                    break;
                case 7:
                    Log.e("限速预警","卡车最小速度" );
                    break;
                case 8:
                    Log.e("限速预警","卡车最大速度" );
                    break;
                    
                case 9:
                    Log.e("限速预警","卡车夜晚最大速度" );
                    break;
                case 10:
                    Log.e("限速预警","出租车最小速度" );
                    break;
                case 11:
                    Log.e("限速预警","出租车最大速度" );
                    break;
                case 12:
                    Log.e("限速预警","出租车最夜晚大速度" );
                    break;

                default:
                    break;
            }
            //限时速度
            String baty2 = s.substring(2, 4);
            int v2 = Integer.parseInt(baty2);
            Log.e("限速",""+v2);
        }

    }


}
